package com.example.student.controller;

import static com.example.student.util.ParamUtils.validation;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.student.constant.ResultCode;
import com.example.student.entity.ResultDO;
import com.example.student.entity.StudentDO;
import com.example.student.service.StudentInterService;
import com.example.student.util.PageUtils;

/**
 * 学生管理前端控制类
 *
 * @author
 */
@RestController
@ComponentScan({"com.example.student.service"})
@RequestMapping("/student")
public class StudentRestController{

    @Resource
    private StudentInterService studentService;

    
    @GetMapping("getStudent")
    public ResultDO<PageUtils<StudentDO>> getStudent(@RequestParam Map<String, Object> params) {
        if (!validation(params)) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        return studentService.getStudent(params);
    }

    
    @PostMapping("insert")
    public ResultDO<Void> insert(@RequestBody StudentDO studentDO) {
        if (StringUtils.isBlank(studentDO.getName())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        return studentService.insert(studentDO);
    }

  
    @PostMapping("delete")
    public ResultDO<Void> delete(@RequestBody StudentDO studentDO) {
        if (StringUtils.isBlank(studentDO.getId())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        return studentService.delete(studentDO.getId());
    }

    @PostMapping("update")
    public ResultDO<Void> update(@RequestBody StudentDO studentDO) {
        if (StringUtils.isBlank(studentDO.getId()) || StringUtils.isBlank(studentDO.getName())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        return studentService.update(studentDO);
    }

}
